package com.qf.util;

import com.qf.bean.Bargaininfo;
import org.apache.poi.hssf.usermodel.*;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class BargainExcelUtil {
    //1.创建一个Excel文件
    static HSSFWorkbook workbook;
    //2.创建文件中的sheet表
    static HSSFSheet sheet;

    //3.创建sheet中的行列信息
    //3.1创建第一行
    public static  void createFirst(String headname){
        workbook=new HSSFWorkbook();
        sheet=workbook.createSheet(headname);
        //创建第一行,0表示第一行
        HSSFRow row=sheet.createRow(0);
        String[] heads={"合同名称","机构名称","合同类型","总金额","经办人","签约时间","到期时间"};
        for(int i=0;i<heads.length;i++){
            //创建列
            HSSFCell cell=row.createCell(i);
            cell.setCellValue(heads[i]);
        }
    }
    //3.2创建除第一行以外的其他行，参数表示的是被导出的数据集合
    public static void createOthers(List<Bargaininfo> list){
        //设置日期格式
        HSSFCellStyle cellStyle=workbook.createCellStyle();
        HSSFDataFormat dataFormat = workbook.createDataFormat();
        cellStyle.setDataFormat(dataFormat.getFormat("yyyy年MM月dd日"));

        for(int i=0;i<list.size();i++){
            HSSFRow row = sheet.createRow(i+1);
            Bargaininfo bargaininfo = list.get(i);
            HSSFCell cella = row.createCell(0);//第一列
            cella.setCellValue(bargaininfo.getBargainname());
            HSSFCell cellb = row.createCell(1);//第2列
            cellb.setCellValue(bargaininfo.getCharityname());
            HSSFCell cellc = row.createCell(2);//第3列
            cellc.setCellValue(bargaininfo.getStylename());
            HSSFCell celld = row.createCell(3);//第4列
            celld.setCellValue(bargaininfo.getCount().toString());
            HSSFCell celle = row.createCell(4);//第5列
            celle.setCellValue(bargaininfo.getOperator());

            HSSFCell cellf = row.createCell(5);//第6列   签约时间
            cellf.setCellValue(bargaininfo.getContracttime());
            cellf.setCellStyle(cellStyle);

            HSSFCell cellg = row.createCell(6);//第7列   到期时间
            cellg.setCellValue(bargaininfo.getEndtime());
            cellg.setCellStyle(cellStyle);
        }
    }

    //4.设置导出信息
    public static void export(OutputStream outputStream) throws IOException {
        //设置以网格的方式输出
        sheet.setGridsPrinted(true);
        //将文件写入到输出流中
        workbook.write(outputStream);
    }

}
